package com.wehealth;


import java.util.ArrayList;
import java.util.List;

import com.wehealth.adapter.Adapter_questionnaire_page2;
import com.wehealth.adapter.Adapter_questionnaire_page3;
import com.wehealth.dm.DM_Questionnaire_page2_item;
import com.wehealth.dm.DM_Questionnaire_page3_item;
import com.wehealth.util.QuestionnaireDataManager;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;


@SuppressLint("ValidFragment")
public class Questionnaire_page3 extends Fragment {

	private MainActivity context;
    private List<DM_Questionnaire_page3_item>  mItemList;
	private QuestionnaireDataManager  mQuestionnaireDataManage;

	
	private String[] items_raido5 ;
	private String[] items_raido9 ;
	
	private int[] mitemNamesId = {
			R.string.questionaire_page3_item1,
			R.string.questionaire_page3_item2,
			R.string.questionaire_page3_item3,
			R.string.questionaire_page3_item4,
			R.string.questionaire_page3_item5,
			R.string.questionaire_page3_item6,
			R.string.questionaire_page3_item7,
			R.string.questionaire_page3_item8,
			R.string.questionaire_page3_item9,
			R.string.questionaire_page3_item10,
			};
	
	private int[][] mitemNamesId2 = {
			{R.string.questionaire_page3_item1_radio1,R.string.questionaire_page3_item1_radio2,R.string.questionaire_page3_item1_radio3,R.string.questionaire_page3_item1_radio4},
			{R.string.questionaire_page3_item2_radio1,R.string.questionaire_page3_item3_radio2,R.string.questionaire_page3_item2_radio3,R.string.questionaire_page3_item2_radio4},
			{R.string.questionaire_page3_item3_radio1,R.string.questionaire_page3_item3_radio2,R.string.questionaire_page3_item3_radio3,R.string.questionaire_page3_item3_radio4},
			{R.string.questionaire_page3_item4_radio1,R.string.questionaire_page3_item4_radio2,R.string.questionaire_page3_item4_radio3,R.string.questionaire_page3_item4_radio4},
			{R.string.questionaire_page3_item5_radio1,R.string.questionaire_page3_item5_radio2,R.string.questionaire_page3_item5_radio3,R.string.questionaire_page3_item5_radio4,},
			{R.string.questionaire_page3_item6_radio1,R.string.questionaire_page3_item6_radio2,R.string.questionaire_page3_item6_radio3,R.string.questionaire_page3_item6_radio4},
			{R.string.questionaire_page3_item7_radio1,R.string.questionaire_page3_item7_radio2,R.string.questionaire_page3_item7_radio3,R.string.questionaire_page3_item7_radio4},
			{R.string.questionaire_page3_item8_radio1,R.string.questionaire_page3_item8_radio2,R.string.questionaire_page3_item8_radio3,R.string.questionaire_page3_item8_radio4},
			{R.string.questionaire_page3_item9_radio1,R.string.questionaire_page3_item9_radio2,R.string.questionaire_page3_item9_radio3,R.string.questionaire_page3_item9_radio4},
			{R.string.questionaire_page3_item10_radio1,R.string.questionaire_page3_item10_radio2,R.string.questionaire_page3_item10_radio3,R.string.questionaire_page3_item10_radio4}
	};

	public  Questionnaire_page3()
	{
		Log.i("init:", "Message"); 

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return  inflater.inflate(R.layout.questionnaire_page3, null);
	}
	
	
	
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		
		context =(MainActivity)getActivity();

		mQuestionnaireDataManage = QuestionnaireDataManager.getInstance();	
		TextView title=(TextView)context.findViewById(R.id.header_title);
		String titlename = context.getString(R.string.questionnaire_title);
		title.setText(titlename);
		Button bBack = (Button) context.findViewById(R.id.header_bBack);
		bBack.setVisibility(View.GONE);
		
		items_raido5 = context.getResources().getStringArray(R.array.questionnaire_page3_item5_radios);
		items_raido9	= context.getResources().getStringArray(R.array.questionnaire_page3_item9_radios);
		
		ListView mListview = (ListView)context.findViewById(R.id.questionnair_page3_list);
		mItemList = new ArrayList<DM_Questionnaire_page3_item>();
		
		int lenght = mitemNamesId.length;
		for(int i =0 ; i < lenght; i++){
			DM_Questionnaire_page3_item  item = new DM_Questionnaire_page3_item();
            String titles = getString(mitemNamesId[i]);
            item.setTitle(titles);
            int leanght2 = mitemNamesId2[i].length;
            List<String>  raidos = new ArrayList<String>();
           if(i!=4 && i!=8){
	            for(int j=0; j<leanght2; j++ ){
	            	String  radio = getString(mitemNamesId2[i][j]);
	            	raidos.add(radio);
	            }
           }else{
        	   int leanght3;
        	   if(i==4){
        		    leanght3 =items_raido5.length;
        		    Log.d("SIMMON","  5 leanght3 ="+leanght3);
        		    for(int k = 0 ; k < leanght3; k++){
    	            	String  radio =items_raido5[k];
    	            	raidos.add(radio);
        		    }
        	   }
        	   if(i==8){
       		    	leanght3 =items_raido9.length;
       		    	Log.d("SIMMON","  leanght 9 ="+leanght3);
       		    	for(int k = 0 ; k < leanght3; k++){
   	            	String  radio =items_raido9[k];
   	            	raidos.add(radio);
       		    }
        	   }
           }
            item.setListRadio(raidos);
            mItemList.add(item);
		}
		
		Adapter_questionnaire_page3  adpater = new Adapter_questionnaire_page3(context,mItemList);
		mListview.setAdapter(adpater);

		Button questionnaire_page3_next_bt = (Button)context.findViewById(R.id.questionnaire_page3_next_bt);
		questionnaire_page3_next_bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				int score3 = 0;
				int count = mItemList.size();
				List<String>  mList = mQuestionnaireDataManage.getStringList();
				
				for(int k=0; k < count ;k++){
					score3 += mItemList.get(k).getScore();
					String  descript = mItemList.get(k).getDisplay();
					Log.d("SIMMON","  k    descript= "+descript);
					if(descript !=null){
						mList.add(descript);
					}
				}
				Log.d("SIMMON"," score ="+score3);
				mQuestionnaireDataManage.setScore3(score3);
				
				openDialog();
			}
		});

	}

	
	private void openDialog(){
		
		        AlertDialog.Builder builder = new AlertDialog.Builder(context); 
		        builder.setIcon(null);
		        String title = getString(R.string.questionaire_page3_dialog);
		        TextView textview = new TextView(context);
		         textview.setText(title);
		         textview.setTextColor(Color.BLACK);
		         textview.setTextSize(16);
		         textview.setPadding(5, 5, 5, 5);
		         textview.setGravity(Gravity.CENTER_VERTICAL);
		         builder.setView(textview);
		         builder.setPositiveButton("查看结果", new AlertDialog.OnClickListener() {
		             @Override
		             public void onClick(DialogInterface arg0, int arg1) {
		                 // TODO Auto-generated method stub
						   Questionnaire_page4 page4 = new Questionnaire_page4();
						   context.switchContent(page4);
		             }
		             
		         });
		         builder.create().show();
		         
		     }

}
